package com.backstage.controller.register;


import com.backstage.entity.UserEntity;
import com.backstage.service.register.RegisterService;
import com.backstage.util.Md5Util;
import com.backstage.util.ReturnUtil;
import com.backstage.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * Created by cyn1c on 2018/6/10.
 */
@RestController
public class RegisterController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    RegisterService registerService;

    @RequestMapping("/static/register")
    public Map register() {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        if (StringUtil.isEmpty(userName) || StringUtil.isEmpty(password)) {
            return ReturnUtil.resErrorMap("参数错误");
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setId(UUID.randomUUID().toString().replaceAll("-", ""));//主键id
        userEntity.setUserName(userName);//用户名
        userEntity.setSalt(Md5Util.generateSalt());//盐值
        userEntity.setPassword(Md5Util.EncoderByMd5(Md5Util.EncoderByMd5(new String(password)).toLowerCase() + userEntity.getSalt()).toLowerCase());//加密后的密码
        userEntity.setPhone(phone);//电话号码
        userEntity.setStatus("0");//设置状态 0:未删除
        userEntity.setAddTime(new Date());
        int res = registerService.registerAccount(userEntity);
        if (res >= 1) {
            return ReturnUtil.resSuccesssMap("注册成功", "");
        } else {
            return ReturnUtil.resErrorMap("未知异常");
        }
    }
}
